#include "TimeEditForm.h"
#include "ui_TimeEditForm.h"

TimeEditForm::TimeEditForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TimeEditForm),m_Index(0),m_totalSeconds(0)
{
    ui->setupUi(this);
}

TimeEditForm::~TimeEditForm()
{
    delete ui;
}

void TimeEditForm::setValue(quint64 seconds)
{
    ui->spinBox->setValue(seconds);

    // 将秒转换为 QTime
    int hours = seconds / 3600;
    int remainingSeconds = seconds % 3600;
    int minutes = remainingSeconds / 60;
    int secs = remainingSeconds % 60;

    m_totalSeconds = seconds;
    ui->timeEdit->setTime(QTime(hours, minutes, secs));
}

void TimeEditForm::setIndex(int index)
{
    m_Index = index;
}

quint64 TimeEditForm::value()
{
    return  m_totalSeconds;
}

int TimeEditForm::index()
{
   return m_Index;
}

void TimeEditForm::setTimeFormat(int format)
{
    switch (format) {
    case 0:
        ui->stackedWidget->setCurrentIndex(0);
        break;
    case 1:
        ui->stackedWidget->setCurrentIndex(1);
        break;
    default:
        break;
    }
}

void TimeEditForm::on_timeEdit_timeChanged(const QTime &time)
{
    // 获取小时、分钟和秒
    int hours = time.hour();
    int minutes = time.minute();
    int seconds = time.second();

    // 将 QTime 转换为总秒数
    m_totalSeconds = hours * 3600 + minutes * 60 + seconds;
    ui->spinBox->setValue(m_totalSeconds);
}

void TimeEditForm::on_spinBox_valueChanged(int arg1)
{
    m_totalSeconds = arg1;
    // 将秒转换为 QTime
    int hours = arg1 / 3600;
    int remainingSeconds = arg1 % 3600;
    int minutes = remainingSeconds / 60;
    int secs = remainingSeconds % 60;

    ui->timeEdit->setTime(QTime(hours, minutes, secs));
    emit sig_ValueChanged(m_Index,m_totalSeconds);
}
